{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": true,
    "editable": true
   },
   "source": [
    "# Polynomial Regression"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": true,
    "editable": true
   },
   "source": [
    "What if your data doesn't look linear at all? Let's look at some more realistic-looking page speed / purchase data:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<matplotlib.collections.PathCollection at 0x16cb83bd978>"
      ]
     },
     "execution_count": 1,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAXoAAAD8CAYAAAB5Pm/hAAAABHNCSVQICAgIfAhkiAAAAAlwSFlz\nAAALEgAACxIB0t1+/AAAIABJREFUeJzt3X9w3PV95/HnW2INa+eH8KByWMbYlyHO4BBQUBO3bjP8\naGJ6pFgDKZiBG3qXGd/ccJniUFE5xxxwEw73fEnITCe98RAaZ6BgA47ilCsO5UdJaQ2VkRzFgBsS\nsPECsRJbSbEXey2974/9rrxa7Xd//9J3X4+ZjHa/+93dD7Ly3s++P+/v+2PujoiIRFdHswcgIiL1\npUAvIhJxCvQiIhGnQC8iEnEK9CIiEadALyIScQr0IiIRp0AvIhJxCvQiIhF3WrMHAHDWWWf50qVL\nmz0MEZE5Zffu3b909+5i57VEoF+6dCnDw8PNHoaIyJxiZvtLOU+pGxGRiCsa6M3sATM7ZGY/yTn+\nJTN7zcz2mtn/zjq+wcxeN7N9Zra6HoMWEZHSlZK6+Q7wl8B3MwfM7DJgDXCRux83s98Kjl8ArAVW\nAIuAvzezj7r7ZK0HLiIipSk6o3f354HDOYf/K7DR3Y8H5xwKjq8BHnH34+7+BvA68KkajldERMpU\naY7+o8Dvm9mLZvYPZvbbwfEe4K2s8w4Gx0REpEkqrbo5DVgIrAR+G9hmZv++nBcws3XAOoAlS5ZU\nNIihkQS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jKYWZvQn0ufucqEk3sy3Aj9z9/uDK+/nuPtHscZUi\niHkJ4NPuvr9R79vsGf10O2N3PwFk2hm3LHd/Hjjc7HGUw93fcfeXg9v/BrxKi3cT9bT3grsx0ldY\nH2nikIoys8XAVcD9zR5LVJnZh4HPAN8GcPcTcyXIB64AftbIIA/ND/RqZ9xgZrYU6AVebO5IigvS\nIKPAIeA5d2/1dNl9wO3AVLMHUiYH/t7MdpvZumYPpohlwDjw10GK7H4zW9DsQZVhLfBwo9+02YFe\nGsjMPgA8Dtzq7r9p9niKcfdJd7+YdPfT3zezy5o9pjBm9nngkLvvbvZYKvB7we/5D4FbgvRkqzoN\n+CTwV+7eS7p1Ssuv7QEEaaargUcb/d7NDvRF2xlLbQR57seBh9x9e7PHU47gq/kTpHPfrWoVcHWQ\n734EuNzMHmzukErj7ong5yHge6RTqq3qIHDQ3TPfSB8jHfjngj8EXnb3XzT6jZsd6NXOuAGChc1v\nA6+6+9ebPZ5SmFm3mXUFt+OkF+xHmzuqcO6+wd0Xu/tS0n/Hz7j7TU0eVlFmtiBYoCdIgXwOaNkU\nmbu/C7xlZsuDQ1cwd7YovYEmpG2gfnvGlmQutjM2s4eBS4GzzOwgcKe7f7u5oypqFfAfgbEg5w3w\nFXf/f00cUzHnAFvMrIP0hORBd3+qyWOKorOB76XnApwG/I27P9ncIRX1JeChYHL4c+A/NXk8RQUf\nop8F/ktT3l9XxoqIRFuzUzciIlJnCvQiIhGnQC8iEnEK9CIiEadALyIScQr0IiIRp0AvIhJxCvQi\nIhH3/wGD0WdGAAAABElEQVQznygWV3tUfgAAAABJRU5ErkJggg==\n",
      "text/plain": [
       "<matplotlib.figure.Figure at 0x16cb7f58208>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "%matplotlib inline\n",
    "from pylab import *\n",
    "import numpy as np\n",
    "\n",
    "np.random.seed(2)\n",
    "pageSpeeds = np.random.normal(3.0, 1.0, 1000)\n",
    "purchaseAmount = np.random.normal(50.0, 10.0, 1000) / pageSpeeds\n",
    "\n",
    "scatter(pageSpeeds, purchaseAmount)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": true,
    "editable": true
   },
   "source": [
    "numpy has a handy polyfit function we can use, to let us construct an nth-degree polynomial model of our data that minimizes squared error. Let's try it with a 4th degree polynomial:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [],
   "source": [
    "x = np.array(pageSpeeds)\n",
    "y = np.array(purchaseAmount)\n",
    "\n",
    "p4 = np.poly1d(np.polyfit(x, y, 4))\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": true,
    "editable": true
   },
   "source": [
    "We'll visualize our original scatter plot, together with a plot of our predicted values using the polynomial for page speed times ranging from 0-7 seconds:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAXoAAAD8CAYAAAB5Pm/hAAAABHNCSVQICAgIfAhkiAAAAAlwSFlz\nAAALEgAACxIB0t1+/AAAIABJREFUeJzt3Xl4lOXV+PHvSRjChCUTIEAIq8hSQYGairvgBu75qUV9\nXbC1Ulvb19YWhfatW13oS63t29ZWat2piopxrRS3qhSXQEB2QTBAAiQsCUsGyHJ+f8wkTpLZMksm\nMzmf68qVzDPPzJxEPHPPue/n3KKqGGOMSV1piQ7AGGNMfFmiN8aYFGeJ3hhjUpwlemOMSXGW6I0x\nJsVZojfGmBRnid4YY1KcJXpjjElxluiNMSbFdUp0AAC9e/fWIUOGJDoMY4xJKkuXLt2lqjmhzmsX\niX7IkCEUFRUlOgxjjEkqIlISznlWujHGmBQXMtGLyGMiUi4iq5od/7GIrBOR1SLyvz7HZ4nIRhFZ\nLyKT4xG0McaY8IVTunkC+BPwVMMBEZkEXAKMVdXDItLHe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nOEr0xxqS4/w8SmyYSO7z1OgAA\nAABJRU5ErkJggg==\n",
      "text/plain": [
       "<matplotlib.figure.Figure at 0x16cb63f7320>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "import matplotlib.pyplot as plt\n",
    "\n",
    "xp = np.linspace(0, 7, 100)\n",
    "plt.scatter(x, y)\n",
    "plt.plot(xp, p4(xp), c='r')\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": true,
    "editable": true
   },
   "source": [
    "Looks pretty good! Let's measure the r-squared error:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0.82937663963\n"
     ]
    }
   ],
   "source": [
    "from sklearn.metrics import r2_score\n",
    "\n",
    "r2 = r2_score(y, p4(x))\n",
    "\n",
    "print(r2)\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": true,
    "editable": true
   },
   "source": [
    "## Activity"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": true,
    "editable": true
   },
   "source": [
    "Try different polynomial orders. Can you get a better fit with higher orders? Do you start to see overfitting, even though the r-squared score looks good for this particular data set?"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.5.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
